<?php
$public = false;
if (!isset($common)) require_once 'include/common.php';

require_once 'include/functions.php'; 
require_once  'include/edificios.php';
require_once  'include/unidades.php';
?>

<?php include 'ui/header.php'; ?>

	<script type="text/javascript" src="include/menus/modalbox/prototype.js"></script>  
	<script type="text/javascript" src="include/menus/modalbox/scriptaculous.js?load=effects"></script>  
	<script type="text/javascript" src="include/menus/modalbox/modalbox.js"></script>  
	<link rel="stylesheet" href="include/menus/modalbox/modalbox.css" type="text/css" media="screen" />  

<?php include 'ui/body.php'; ?>

<script type="text/javascript">

function newBuilding(i, j)
{
	Modalbox.show("include/menus/chooseTypeOfBuilding.php?i="+ i +"&j="+ j, {title: this.title, width: 800, height: 550});
}

function improveBuilding(i, j)
{
	Modalbox.show("include/menus/upgradeBuilding.php?i="+ i +"&j="+ j, {title: this.title, width: 800, height: 149});
}

function getRadioValue(radio1, radio2, radio3, radio4, radio5)
{
	Modalbox.hide();
	
	if (radio1 == true)
	{
		type = 0;
	}
	else if (radio2 == true) 
	{
		type = 1;
	}
	else if (radio3 == true)
	{
		type = 2;
	}
	else if (radio4 == true)
	{
		type = 3;
	}
	else if (radio5 == true)
	{
		type = 4;
	}
	
	window.location="city.php?type=" + type;
}

function upgrade(type)
{
	window.location="city.php?type=" + type;
}

function closeModalBox()
{
	Modalbox.hide();
}

function createSoldiers()
{
	Modalbox.show("include/menus/chooseTypeOfSoldiers.php", {title: this.title, width: 800, height: 575});
}


function getTextValue(triarii, legio, equites, sagitarii, praetoriani)
{
	Modalbox.hide();

	window.location="city.php?triarii=" + triarii + "&legio=" + legio + "&equites=" + equites + "&sagitarii=" + sagitarii + "&praetoriani=" + praetoriani;
}

function underConstruction()
{
	alert("Casilla en construcción.");
}

</script>

<h1> <?php echo mb_strtoupper(getNamecity($_SESSION['idcity'][0])) ?> </h1>

<table border="1">
  <tr>
  	<td><img name="grano" style="border:none;" src="images/recursos/grano.GIF" > <?php $resources = getResources($_SESSION['idcity'][0]); echo $resources->getGrano(); ?> </td>      
    <td><img name="grano" style="border:none;" src="images/recursos/hierro.GIF" > <?php echo $resources->getHierro(); ?> </td>
    <td><img name="grano" style="border:none;" src="images/recursos/madera.GIF" > <?php echo $resources->getMadera(); ?> </td>
    <td><img name="grano" style="border:none;" src="images/recursos/oro.GIF" > <?php echo $resources->getOro(); ?> </td>
  </tr>
</table>

<table border="1">
  <tr>
    <td> <?php $soldiers = getSoldiers($_SESSION['idcity'][0]); echo $soldiers->getTriarii()." triarii"; ?> </td>
    <td> <?php echo $soldiers->getLegio()." legio"; ?> </td>
    <td> <?php echo $soldiers->getEquites()." equites"; ?> </td>
    <td> <?php echo $soldiers->getSagitarii()." sagitarii"; ?> </td>
    <td> <?php echo $soldiers->getPraetoriani()." praetoriani"; ?> </td>
  </tr>
</table>

<?php

// Tabla principal. Casillero / Información
echo '<table>';
echo '<tr><td  valign="top">';

	// Tabla del casillero
	echo '<table>';
	
	for ($i = 0; $i < 4; $i++)
	{
		echo '<tr>';
		for ($j = 0; $j < 4; $j++)
		{
			// tipoEd -1 -> Casilla vacia
			if (getBuilding($i, $j, $_SESSION['idcity'][0])->getBox() == -1)
			{
				?>			
					<td><img name="<?php echo $i?>" id="<?php echo $j?>" style="border:none;" src="images/squares/casilla-verde-ciudad.gif" onClick="newBuilding(this.name, this.id);return false"></td>
				<?php
			}
			// Level 0 -> En construcci�n o mejorando
			elseif (isUpgrading($_SESSION['idcity'][0], (4*$i) + $j))
			{	
				if (nextLevel($_SESSION['idcity'][0],  (4*$i) + $j) > 1)
				{
					?>			
						<td><img name="<?php echo $i?>" id="<?php echo $j?>" style="border:none;" src="images/edificios/<?php echo getNameBuilding(getBuilding($i, $j, $_SESSION['idcity'][0])->getBox()); ?>Mej.GIF" onClick="underConstruction()"></td>
					<?php
				}
				else 
				{
					?>			
						<td><img name="<?php echo $i?>" id="<?php echo $j?>" style="border:none;" src="images/edificios/<?php echo getNameBuilding(getBuilding($i, $j, $_SESSION['idcity'][0])->getBox()); ?>Cons.GIF" onClick="underConstruction()"></td>
					<?php
				}
			}
			// tipoEd 0, 1, 2, 3 y 4
			elseif (!isUpgrading($_SESSION['idcity'][0], (4*$i) + $j) && getBuilding($i, $j, $_SESSION['idcity'][0])->getBoxLvl() < 6)
			{
				?>			
					<td><img name="<?php echo $i?>" id="<?php echo $j?>" style="border:none;" src="images/edificios/<?php echo getNameBuilding(getBuilding($i, $j, $_SESSION['idcity'][0])->getBox()); echo getBuilding($i, $j, $_SESSION['idcity'][0])->getBoxLvl(); ?>.GIF" onClick="improveBuilding(this.name, this.id);return false"></td>
				<?php
			}
			// Los demas niveles
			else
			{
				?>			
					<td><img name="<?php echo $i?>" id="<?php echo $j?>" style="border:none;" src="images/edificios/<?php echo getNameBuilding(getBuilding($i, $j, $_SESSION['idcity'][0])->getBox()); ?>.gif" onClick="improveBuilding(this.name, this.id);return false"></td>
				<?php
			}
		}
		echo '</tr>';
	}
	echo '</table>';

echo '</td>';

	// Tabla para información
	echo '<td valign="top">';
	
	// Mostrar información
	echo '<p><b>Recursos producidos por hora:</b></p>';
	
	$resourcesProducted = searchResources($_SESSION['idcity'][0]);
	
	?>	
		<img name="grano" style="border:none;" src="images/recursos/grano.GIF" >
	<?php 	
	printf($resourcesProducted->getGrano()."</p>");
	
	?>	
		<img name="hierro" style="border:none;" src="images/recursos/hierro.GIF" >
	<?php 	
	printf($resourcesProducted->getHierro()."</p>");
	
	?>	
		<img name="madera" style="border:none;" src="images/recursos/madera.GIF" >
	<?php 	
	printf($resourcesProducted->getMadera()."</p>");
	
	?>	
		<img name="oro" style="border:none;" src="images/recursos/oro.GIF" >
	<?php 	
	printf($resourcesProducted->getOro()."</p>");
	
	echo '<p><b>Edificios en construcción:</b></p>';
	
	?> <strong><?php searchBuildings($_SESSION['idcity'][0]);?> </strong> <?php
	
	echo '<p><b>Tropas creándose:</b></p>';
	
	searchSoldiers($_SESSION['idcity'][0]);
	
	echo '<p><b>Ataques lanzados:</b></p>';
	
	searchAttacks($_SESSION['idcity'][0]);
	
	echo '<p><b>Tropas volviendo:</b></p>';
	
	searchReturnings($_SESSION['idcity'][0]);
	
	echo '</td>';

echo '</table>'; 

	
        // COMPROBAR SI SE TIENEN RECURSOS PARA EDIFICIOS 

		if ( isset($_GET['type']) )
		{
			$type = $_GET['type'];
			$resources = getResources($_SESSION['idcity'][0]);
			$coste = GetCoste($type, $_SESSION['level']);
			
			if ($resources->getGrano() >= $coste[1] && $resources->getHierro() >= $coste[2] && $resources->getOro() >= $coste[3] && $resources->getMadera() >= $coste[4])
			{ 
				?>
				<script type="text/javascript">
					window.location="include/dbsave/saveBuilding.php?type=" + <?php echo $type; ?> + "&grano=" + <?php echo $coste[1]; ?> + "&hierro=" + <?php echo $coste[2]; ?> + "&oro=" + <?php echo $coste[3]; ?> + "&madera=" + <?php echo $coste[4]; ?>
				</script>
				<?php 
			}
			else
			{ 
				?>
				<script type="text/javascript">
					alert("No tienes suficientes recursos para crear el edificio.");
					window.location="city.php";
				</script>
				<?php 
			}
		}
		
		// COMPROBAR SI SE TIENEN RECURSOS PARA SOLDADOS 
		
		
		if ( isset($_GET['triarii']) || isset($_GET['legio']) || isset($_GET['equites']) || isset($_GET['sagitarii']) || isset($_GET['praetoriani']) )
		{
				$costeTotal = unidadesGetCoste(0, $_GET['triarii']);
		
				$coste = unidadesGetCoste(1, $_GET['legio']);
				
				for ($i = 0; $i < 5; $i++)
				{
					$costeTotal[$i] = $costeTotal[$i] + $coste[$i];
				}
				
				$coste = unidadesGetCoste(2, $_GET['equites']);
				
				for ($i = 0; $i < 5; $i++)
				{
					$costeTotal[$i] = $costeTotal[$i] + $coste[$i];
				}
				
				$coste = unidadesGetCoste(3, $_GET['sagitarii']);
				
				for ($i = 0; $i < 5; $i++)
				{
					$costeTotal[$i] = $costeTotal[$i] + $coste[$i];
				}
				
				$coste = unidadesGetCoste(4, $_GET['praetoriani']);
				
				for ($i = 0; $i < 5; $i++)
				{
					$costeTotal[$i] = $costeTotal[$i] + $coste[$i];
				}
				
			
			if ($resources->getGrano() >= $costeTotal[1] && $resources->getHierro() >= $costeTotal[2] && $resources->getOro() >= $costeTotal[3] && $resources->getMadera() >= $costeTotal[4])
			{ 
				?>
				<script type="text/javascript">
					window.location="include/dbsave/saveSoldiers.php?triarii=" + <?php echo $_GET['triarii'] + 1; ?>  + "&legio=" + <?php echo $_GET['legio'] + 1; ?> + "&equites=" + <?php echo $_GET['equites'] + 1; ?> + "&sagitarii=" + <?php echo $_GET['sagitarii'] + 1; ?> + "&praetoriani=" + <?php echo $_GET['praetoriani'] + 1; ?> + "&grano=" + <?php echo $costeTotal[1]; ?> + "&hierro=" + <?php echo $costeTotal[2]; ?> + "&oro=" + <?php echo $costeTotal[3]; ?> + "&madera=" + <?php echo $costeTotal[4]; ?>
				</script>
				<?php 
			}
			else
			{ 
				?>
				<script type="text/javascript">
					alert("No tienes suficientes recursos para crear los soldados.");
					window.location="city.php";
				</script>
				<?php 
			}
		}

		
?>

<?php // TODO JOSE: Para crear soldados, borrar cuando se ponga en el sitio correcto  
 
if (searchHeadQuarter($_SESSION['idcity'][0]))
{
	echo '<input type="button" value="Crear soldados" onclick="createSoldiers()"/>'; 
}

?>

<?php include 'ui/end.php'; ?>